Skip to content

compile: Support shapeless compile for Scan primitive - #4468

Closed
somuai wants to merge 1 commit into
ml-explore:mainfrom
somuai:fix-scan-shapeless-compile
Closed

somuai wants to merge 1 commit into
ml-explore:mainfrom
somuai:fix-scan-shapeless-compile

Conversation

@somuai

@somuai somuai commented Sep 5, 2026

Copy link
Copy Markdown

Fixes #4460

Motivation

The Scan primitive (backing cumsum, cumprod, cummin, cummax, and cumlogaddexp) did not override Primitive::output_shapes. As a result, compiling a graph containing cumulative operations with shapeless=True failed with:

[Primitive::output_shapes] CumSum cannot infer output shapes.

Because all operations derived from Scan preserve their input tensor shape, Scan::output_shapes can safely return the first input's shape.

Changes

  1. Added DEFINE_INPUT_OUTPUT_SHAPE() to class Scan : public UnaryPrimitive in mlx/primitives.h.
  2. Added unit test coverage in python/tests/test_compile.py verifying dynamic shape execution across 1D and 2D arrays under mx.compile(..., shapeless=True).
  3. Added C++ test case in tests/compile_tests.cpp asserting dynamic shape compilation with cumsum.

Fixes ml-explore#4460

The Scan primitive (backing cumsum, cumprod, cummin, cummax, and
cumlogaddexp) was missing an output_shapes definition, causing
mx.compile(..., shapeless=True) to fail with:
[Primitive::output_shapes] CumSum cannot infer output shapes.

Add DEFINE_INPUT_OUTPUT_SHAPE() to Scan so that it preserves the
first input's shape during shapeless graph compilation. Added
Python and C++ unit tests verifying dynamic shape execution.

Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>
@JasonHonKL

Copy link
Copy Markdown
Contributor

@somuai mind to tell which AI model you are using lollll.

@zcbenz

zcbenz commented Sep 6, 2026

Copy link
Copy Markdown
Member

Closing for violating contributing guidelines.

@zcbenz zcbenz closed this Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scan primitive lacks output_shapes for shapeless compile

3 participants